﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BookShop.DAL;
using BookShop.DTO;

namespace BookShop.BLL
{
    public class OrderDetailService
    {
        public static OrderDetail Add(OrderDetail orderDetail)
        {
            using (var repository = new OrderDetailRepository())
            {
                return repository.Add(orderDetail);
            }
        }

        public static bool Delete(OrderDetail orderDetail)
        {
            using (var repository = new OrderDetailRepository())
            {
                return repository.Delete(orderDetail);
            }
        }

        public static OrderDetail Update(OrderDetail orderDetail)
        {
            using (var repository = new OrderDetailRepository())
            {
                orderDetail = repository.Update(orderDetail);
                if (orderDetail != null)
                {
                    var order = OrderService.GetById(orderDetail.OrderId);
                    if (order != null)
                    {
                        order.TotalPrice = OrderService.GetTotalPrice(order.Id);
                        OrderService.Update(order);
                    }
                }
                return orderDetail;
            }
        }

        public static OrderDetail GetById(long orderDetailId)
        {
            using (var repository = new OrderDetailRepository())
            {
                return repository.Find(d => d.Id == orderDetailId);
            }
        }

        public static List<OrderDetail> GetByOrderId(long orderId)
        {
            using (var repository = new OrderDetailRepository())
            {
                var order = OrderService.GetById(orderId);
                return order != null
                    ? repository.FindAll(d => d.OrderId == orderId).ToList()
                    : new List<OrderDetail>();
            }
        }
        
        public static List<OrderDetail> Search(long? orderDetailId, long? bookId, long? orderId, string fromTotalPrice,
            string toTotalPrice, int page, int pageSize, out int count)
        {
            var repository = new OrderDetailRepository();
            var returnList = new List<OrderDetail>();
            if (orderDetailId.HasValue)
            {
                var orderDetail = GetById(orderDetailId.Value);
                if (orderDetail != null)
                {
                    returnList.Add(orderDetail);
                }
            }
            else
            {
                returnList = repository.GetAll().ToList();
                if (bookId.HasValue)
                {
                    returnList = returnList.FindAll(d => d.BookId == bookId.Value);
                }
                if (orderId.HasValue)
                {
                    returnList = returnList.FindAll(d => d.BookId == orderId.Value);
                }
                double newFromTotalPrice;
                var isValidFromTotalPrice = double.TryParse(fromTotalPrice, NumberStyles.AllowDecimalPoint,
                    CultureInfo.CreateSpecificCulture("en-US"), out newFromTotalPrice);
                double newToTotalPrice;
                var isValidToTotalPrice = double.TryParse(toTotalPrice, NumberStyles.AllowDecimalPoint,
                    CultureInfo.CreateSpecificCulture("en-US"), out newToTotalPrice);
                if (isValidFromTotalPrice && isValidToTotalPrice)
                {
                    returnList =
                        returnList.FindAll(o => o.TotalPrice >= newFromTotalPrice && o.TotalPrice <= newToTotalPrice);
                }
                if (isValidFromTotalPrice && !isValidToTotalPrice)
                {
                    returnList =
                        returnList.FindAll(o => o.TotalPrice >= newFromTotalPrice);
                }
                if (!isValidFromTotalPrice && isValidToTotalPrice)
                {
                    returnList =
                        returnList.FindAll(o => o.TotalPrice <= newToTotalPrice);
                }
            }
            count = returnList.Count;
            return returnList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }
    }
}
